package org.zffc.filter;

/**
* 全局拦截器，作用所有的微服务
*
* 1.重写StripPrefix(获取真实的URL)
 * 原始路径：/api/user/profile，重写为/user/profile
* 2.将现在的request，添加当前身份
 * Authorization-UserName：空值（预留字段）
* @author: scott
* @date: 2022/4/8 10:55
*/
//@Slf4j
//@Component
//public class GlobalAccessTokenFilter implements GlobalFilter, Ordered {
//    public final static String X_ACCESS_TOKEN = "X-Access-Token";
//    public final static String X_GATEWAY_BASE_PATH = "X_GATEWAY_BASE_PATH";
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//
//        String scheme = exchange.getRequest().getURI().getScheme();
//        String host = exchange.getRequest().getURI().getHost();
//        int port = exchange.getRequest().getURI().getPort();
//        //update-begin---author:chenrui ---date:20240603  for：地址中没有带端口(http/https默认)时port是-1------------
//        String basePath = scheme + "://" + host;
//        if (port != -1) {
//            basePath += ":" + port;
//        }
//        //update-end---author:chenrui ---date:20240603  for：地址中没有带端口(http/https默认)时port是-1------------
//        // 1. 重写StripPrefix(获取真实的URL)
//        addOriginalRequestUrl(exchange, exchange.getRequest().getURI());
//        String rawPath = exchange.getRequest().getURI().getRawPath();
//        String newPath = "/" + Arrays.stream(StringUtils.tokenizeToStringArray(rawPath, "/")).skip(1L).collect(Collectors.joining("/"));
//        ServerHttpRequest newRequest = exchange.getRequest().mutate().path(newPath).build();
//        exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newRequest.getURI());
//        //2.将现在的request，添加当前身份
//        ServerHttpRequest mutableReq = exchange.getRequest().mutate().header("Authorization-UserName", "").header(X_GATEWAY_BASE_PATH,basePath).build();
//        ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
//        return chain.filter(mutableExchange);
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//
//}
